{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7a1bc647",
   "metadata": {},
   "source": [
    "# 安装"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f943a3e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install easyocr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d719ac77",
   "metadata": {},
   "source": [
    "# 读取图像"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12b2f655",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import cv2\n",
    "from matplotlib import pyplot as plt\n",
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import easyocr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee218a86",
   "metadata": {},
   "source": [
    "https://www.kaggle.com/andrewmvd/car-plate-detection"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38b6f035",
   "metadata": {},
   "source": [
    "![car4](car4.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "06ed3f0c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "img = cv2.imread(\"car4.jpg\")\n",
    "gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n",
    "\n",
    "img2 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n",
    "\n",
    "# car_plate_haar_cascade = cv2.CascadeClassifier('./haarcascade_licence_plate_rus_16stages.xml')\n",
    "car_plate_haar_cascade = cv2.CascadeClassifier('./haarcascade_russian_plate_number.xml')\n",
    "\n",
    "car_plate_rects = car_plate_haar_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5)\n",
    "for x, y, w, h in car_plate_rects:\n",
    "    img2 = cv2.rectangle(img2,(x,y),(x+w,y+h),(0,255,0),5)\n",
    "    car_plate_img = gray[y:y+h ,x:x+w]\n",
    "\n",
    "plt.subplot(1,2,1)\n",
    "plt.imshow(car_plate_img)\n",
    "plt.subplot(1,2,2)\n",
    "plt.imshow(img2)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3cf6a27d",
   "metadata": {},
   "outputs": [],
   "source": [
    "scale_percent = 150\n",
    "width = int(car_plate_img.shape[1] * scale_percent / 100)\n",
    "height = int(car_plate_img.shape[0] * scale_percent / 100)\n",
    "dim = (width, height)\n",
    "resized_image = cv2.resize(car_plate_img, dim, interpolation = cv2.INTER_AREA)\n",
    "\n",
    "blur = cv2.blur(resized_image,ksize=(5,5))\n",
    "\n",
    "plt.imshow(blur)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ede0650",
   "metadata": {},
   "outputs": [],
   "source": [
    "reader = easyocr.Reader(['en'])\n",
    "result = reader.readtext(resized_image, paragraph=True)\n",
    "result"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.11"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
